package com.lihepeng.leecode.heap;

public class HeapSort {
    public void heapSort(int[] arrs) {
        for (int i = arrs.length / 2 - 1; i >= 0; i--) {
            buildMaxHeap(arrs, 0, arrs.length);
        }
        for (int j = arrs.length - 1; j >= 0; j--) {
            swap(arrs, 0, j);
            buildMaxHeap(arrs, 0, j);
        }

    }

    private void swap(int[] arrs, int i, int j) {
        int temp = arrs[i];
        arrs[i] = arrs[j];
        arrs[j] = temp;
    }

    // 构建大堆顶
    private void buildMaxHeap(int[] arrs, int min, int max) {
        int temp = arrs[min];
        for (int k = 2 * min + 1; k < max; k = k * 2 + 1) {
            if (k + 1 < max && arrs[k] < arrs[k + 1]) {
                // 交换顺序
                k = k + 1;
            }
            if (arrs[k] > temp) {
                arrs[min] = arrs[k];
                min = k;
            } else {
                break;
            }
        }
        arrs[min] = temp;
    }

    // 构建小堆顶
    private void buildMinHeap(int[] arrs, int i, int length) {
    }


}
